<?php
/**
 * Created by PhpStorm.
 * User: iceberg
 * Date: 2018-09-14
 * Time: 14:46
 */

namespace app\validate;


use think\Validate;

class FormVerify extends Validate
{
    //是否批量验证
    protected $batchValidate = true;

    //验证规则
    protected $rule = [
        'username' => 'require|min:4|checkNoChinese:用户',
        'password' => 'require|min:6|checkNoChinese:密码',
        'vertify'  => 'require|checkCaptcha',
    ];

    //验证信息
    protected $message = [
        'username' => [
            'require' => '用户名不能为空',
            'min'     => '用户最少要4个字符'
        ],
        'password' => [
            'require' => '密码不能为空',
            'min'     => '密码最少要6个字符'
        ],
        'vertify'   => [
            'require' =>'验证码不能为空',
        ],


    ];
    //检验是否含有中文
    protected function checkNoChinese($value, $msg,$data = [])
    {
        if(!preg_match("/^[A-Za-z0-9]+$/",$value))
        {
            return $msg."不能包含中文和特殊字符";
        }
        return true;
    }

    //验证验证码内容
    public function checkCaptcha($value,$msg,$data = [])
    {
        if(!captcha_check($value)){
            return '验证码错误';
        }
        return true;
    }
}